/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package data.impl;

import data.DataLog;
import data.DataPackage;
import data.DataSample;

import data.Device;
import java.util.Collection;
import java.util.Date;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Log</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link data.impl.DataLogImpl#getStartTime <em>Start Time</em>}</li>
 *   <li>{@link data.impl.DataLogImpl#getDataSamples <em>Data Samples</em>}</li>
 *   <li>{@link data.impl.DataLogImpl#getDevice <em>Device</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class DataLogImpl extends EObjectImpl implements DataLog {
	/**
	 * The default value of the '{@link #getStartTime() <em>Start Time</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartTime()
	 * @generated
	 * @ordered
	 */
	protected static final Date START_TIME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getStartTime() <em>Start Time</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartTime()
	 * @generated
	 * @ordered
	 */
	protected Date startTime = START_TIME_EDEFAULT;

	/**
	 * The cached value of the '{@link #getDataSamples() <em>Data Samples</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataSamples()
	 * @generated
	 * @ordered
	 */
	protected EList<DataSample> dataSamples;

	/**
	 * The cached value of the '{@link #getDevice() <em>Device</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDevice()
	 * @generated
	 * @ordered
	 */
	protected Device device;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected DataLogImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return DataPackage.Literals.DATA_LOG;
	}

	public Date getEndTime()
	{
		Date result = new Date(0);
		if (!this.getDataSamples().isEmpty())
		{
			result = this.getDataSamples().get(this.getDataSamples().size()-1).getTimestamp();
		}
		
		return(result);
	}
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 */
	@Override
	public Date getStartTime() {
		Date result = new Date(0);
		if (!this.getDataSamples().isEmpty())
		{
			result = this.getDataSamples().get(0).getTimestamp();
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setStartTime(Date newStartTime) {
		Date oldStartTime = startTime;
		startTime = newStartTime;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, DataPackage.DATA_LOG__START_TIME, oldStartTime, startTime));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<DataSample> getDataSamples() {
		if (dataSamples == null) {
			dataSamples = new EObjectContainmentEList<DataSample>(DataSample.class, this, DataPackage.DATA_LOG__DATA_SAMPLES);
		}
		return dataSamples;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Device getDevice() {
		if (device != null && device.eIsProxy()) {
			InternalEObject oldDevice = (InternalEObject)device;
			device = (Device)eResolveProxy(oldDevice);
			if (device != oldDevice) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, DataPackage.DATA_LOG__DEVICE, oldDevice, device));
			}
		}
		return device;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Device basicGetDevice() {
		return device;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDevice(Device newDevice) {
		Device oldDevice = device;
		device = newDevice;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, DataPackage.DATA_LOG__DEVICE, oldDevice, device));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case DataPackage.DATA_LOG__DATA_SAMPLES:
				return ((InternalEList<?>)getDataSamples()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case DataPackage.DATA_LOG__START_TIME:
				return getStartTime();
			case DataPackage.DATA_LOG__DATA_SAMPLES:
				return getDataSamples();
			case DataPackage.DATA_LOG__DEVICE:
				if (resolve) return getDevice();
				return basicGetDevice();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case DataPackage.DATA_LOG__START_TIME:
				setStartTime((Date)newValue);
				return;
			case DataPackage.DATA_LOG__DATA_SAMPLES:
				getDataSamples().clear();
				getDataSamples().addAll((Collection<? extends DataSample>)newValue);
				return;
			case DataPackage.DATA_LOG__DEVICE:
				setDevice((Device)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case DataPackage.DATA_LOG__START_TIME:
				setStartTime(START_TIME_EDEFAULT);
				return;
			case DataPackage.DATA_LOG__DATA_SAMPLES:
				getDataSamples().clear();
				return;
			case DataPackage.DATA_LOG__DEVICE:
				setDevice((Device)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case DataPackage.DATA_LOG__START_TIME:
				return START_TIME_EDEFAULT == null ? startTime != null : !START_TIME_EDEFAULT.equals(startTime);
			case DataPackage.DATA_LOG__DATA_SAMPLES:
				return dataSamples != null && !dataSamples.isEmpty();
			case DataPackage.DATA_LOG__DEVICE:
				return device != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (startTime: ");
		result.append(startTime);
		result.append(')');
		return result.toString();
	}
	
	
	public DataSample getMidpointSample(DataSample s0, DataSample s1)
	{
		DataSample	result = null;
		DataSample 	tmp;
		
		if (this.getDataSamples().indexOf(s1) < this.getDataSamples().indexOf(s0)) // s1 must be later than s0
		{
			tmp = s1;
			s1 = s0;
			s0 = tmp;
		}
		
		result = dataSamples.get(((this.getDataSamples().indexOf(s1) - this.getDataSamples().indexOf(s0)) / 2)+this.getDataSamples().indexOf(s0));
		
		return(result);
	}

	public DataSample findNearestSample(DataSample ds) //divide and conquer
	{
		//TODO: this needs to be looked at
		DataSample result 	= null;//this.getDataSamples().get(0);
		DataSample low 		= this.getDataSamples().get(0);
		DataSample high = low;
		
		if (!this.getDataSamples().isEmpty())
		{
			high = this.getDataSamples().get(getDataSamples().size()-1);
		
			
		
			DataSample midpoint = this.getMidpointSample(low,high);
			
			
			if ((low.getTimestamp().getTime() <= ds.getTimestamp().getTime())
		       && (ds.getTimestamp().getTime() <= high.getTimestamp().getTime()))
			{
				while((this.getDataSamples().indexOf(high) - this.getDataSamples().indexOf(low)) > 1)
				{
					midpoint = getMidpointSample(low,high);
					
					if (midpoint.getTimestamp().getTime() >= ds.getTimestamp().getTime())
						high = midpoint;
					else
						low = midpoint;
				}
				
				// There exists 0 values between high & low, and high low maybe =
				
				if (high.equals(low))  // found exact match
					result = high;
				else  // did not find an exact match, find closest match
				{
					if (getDeltaTime(ds,low) < getDeltaTime(ds,high))
						result = low;
					else
						result = high;
				}
			}
			else result = low;
		}
		else
		{
			System.out.println("ERROR: Empty DataLog");
		}
		return(result);
	}
	
	private long getDeltaTime(DataSample s0, DataSample s1)
	{
		long result = 0;
		result = java.lang.Math.abs(s0.getTimestamp().getTime() - s1.getTimestamp().getTime());

		return(result);
	}


} //DataLogImpl
